// pages/authstore/index.js
const {
  GetCate,
  AddTask,
  ShopStore,
  MyShopShow,
  ShopShow,
  HomeListDetail,
  ShopUpdate,
  taskUpdate
} = require('../../utils/api')
import confg from '../../utils/config';
import util from '../../utils/util';

let t = util.timeFormat(new Date(), 'Y-M-D')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    confg: confg,
    task_level: 0,
    cur_task_type: {},
    forms: {},
    catelist: [],
    region: t,
    date_start: t,
    imgurl: [],
    myshop: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    /**
     * options.lv 1 为广场任务 2 为商家
     */
    if (options.lv == 1) {
      confg.task_type.forEach(el => {
        if (el.id == options.id) {
          this.setData({
            cur_task_type: el
          })
        }
      })
    } else if (options.lv == 2) {
      MyShopShow({id:options.shopid}).then(res=>{
        let data = res.data;
        if(res.code==200){
          this.setData({
            myshop:{
              id:options.shopid,
              shop_name:data.shop_name,
              name:data.name,
              images_url:'/images/shop/'+ data.slug +'.png'
            }
          })
        }
      })
    }
    this.setData({
      task_level: options.lv,
      forms: {
        numbers: 10
      }
    })
    this.getCate({
      type: options.lv
    })
    if(options.cid){
      this.setData({
        cid: options.cid
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 添加图片
   */
  uploadimg(e) {
    this.setData({
      imgurl: e.detail.imgurl
    })
  },
  
  /**
   * 删除图片
   */
  delimg() {
    this.setData({
      imgurl: []
    })
  },
  formSubmit(e) {
    let val = e.detail.value;
    let sendobj = {};
    for (let k in val) {
      if (k == 'domain' || k == 'pay_type') {
        sendobj[k] = val[k].join()
      } else {
        sendobj[k] = val[k];
      }
    }
    sendobj['platform'] = this.data.cur_task_type.id;
    sendobj['images'] = this.data.imgurl.join(',');
    if(this.data.myshop){
      sendobj['e_commerce'] = this.data.myshop.id;
      sendobj['shop_name'] = this.data.myshop.shop_name;
    }
    this.ajaxFn(sendobj, (res) => {
      if (res.code == 200) {
        wx.showToast({
          title: res.info,
          icon: 'success',
          duration: 1500
        })
        if (this.data.task_level == 1) {
          wx.switchTab({
            url: '/pages/index/index',
          })
        } else if (this.data.task_level == 2) {
          wx.switchTab({
            url: '/pages/merchant/merchant',
          })
        }
      } else {
        wx.showToast({
          title: res.info,
          icon: 'none',
          duration: 1500
        })
      }
    });
  },
  ajaxFn(obj, cb) {
    if(this.data.cid){
      obj['id'] = this.data.cid;
      if (this.data.task_level == 1) {
        taskUpdate(obj).then(res => {
          cb(res)
        })
      } else if (this.data.task_level == 2) {
        ShopUpdate(obj).then(res => {
          cb(res)
        })
      }
    }else{
      if (this.data.task_level == 1) {
        AddTask(obj).then(res => {
          cb(res)
        })
      } else if (this.data.task_level == 2) {
        ShopStore(obj).then(res => {
          cb(res)
        })
      }
    }
  },
  formReset() {
    console.log('form发生了reset事件')
  },
  getsf(res,cc){
    this.setData({
      forms: cc.data
    })
    let catel = cc.data.domain;
    let catel2 = res.data;
    catel2.forEach((el2)=>{
      let bt1 = false; 
      catel.forEach((el)=>{
        if(el2.id==el.id){
          bt1 = true
        }
      })
      el2['check'] = bt1;
    })
    let drop_type = this.data.confg.drop_type;
    drop_type.forEach((el)=>{
      let bt1 = false; 
      if(el.id==cc.data.pay_type){ bt1 = true }
      el['check'] = bt1;
    })
    this.setData({
      'confg.drop_type': drop_type,
      imgurl: cc.data.images,
      region: cc.data.end_time,
      catelist: catel2
    })
  },
  /**
   * 获取产品领域
   */
  getCate(ob) {
    GetCate(ob).then(res => {
      console.log(this.data.cid)
      if(this.data.cid){
        if (this.data.task_level == 1) {
          HomeListDetail({id:this.data.cid}).then(cc => {
            this.getsf(res,cc)
          })
        } else if (this.data.task_level == 2) {
          ShopShow({id:this.data.cid}).then(cc => {
            this.getsf(res,cc)
          })
        }
        
      }else{
        this.setData({
          catelist: res.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 选择地区
   */
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  /**
   * 获取手机号
   */
  getPhone: function (e) {
    console.log(e)
    // this.setData({
    //   region: e.detail.value
    // })
  },
})